package com.haochang.chunkvod.view.vod.activity;

import java.util.HashMap;

import com.haochang.base.BaseActivity;
import com.haochang.base.BaseEditText;
import com.haochang.base.BaseTextView;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.common.VodConstant;
import com.haochang.chunkvod.app.common.intent.IntentKey;
import com.haochang.chunkvod.app.utils.SoftKeyboardUtils;
import com.haochang.chunkvod.app.utils.ToastUtils;
import com.haochang.chunkvod.model.custom.CustomDialogData;
import com.haochang.chunkvod.model.order.GoodsInfo;
import com.haochang.chunkvod.view.vod.dialog.KeyBoardDialog;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

/**
 * @author qintao
 *         created at 2016/6/7 18:47
 *         用activity自定义半透明的dialog样式
 */

public class CustomDialogActivity extends BaseActivity implements KeyBoardDialog.KeyBoardOnSureListener
{

	private BaseTextView tv_title;
	private BaseTextView tv_name;
	private BaseTextView tv_left_title;
	private BaseEditText et_title_content;
	private BaseEditText tv_content;
	private Button btn_Cancel;
	private Button btn_sure;
	private ImageView img_close;
	private BaseTextView tv_cut_1;
	private BaseTextView tv_add_1;
	private String titleName = "";
	private String leftName = "";
	private String contentName = "";
	private String type = "";
	private GoodsInfo goodInfo = null;
	private String hint = "";
	private String roomId = "";
	private String service_goods_id = "";
	private String search_type = "";
	private String billId = "";
	private String flow_code;
	private String unit;
	private int output_status;

	/**
	 * 当前数量
	 */
	private int currentNumber = 0;

	/**
	 * 最大可退数量
	 */
	private int maxBackNumber = 0;
	private CustomDialogData customDialogData;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
	}

	@Override
	public void receiveParam()
	{
		goodInfo = (GoodsInfo) getIntent().getSerializableExtra(IntentKey.GOODSINFO);
		titleName = getIntent().getStringExtra(IntentKey.CUSTOM_DIALOG_TITLE);
		service_goods_id = getIntent().getStringExtra(IntentKey.SERVICE_GOODS_ID);
		leftName = getIntent().getStringExtra(IntentKey.CUSTOM_DIALOG_LEFT_NAME);
		contentName = getIntent().getStringExtra(IntentKey.CUSTOM_DIALOG_CONTENT);
		type = getIntent().getStringExtra(IntentKey.CUSTOM_DIALOG_TYPE);
		hint = getIntent().getStringExtra(IntentKey.CUSTOM_DIALOG_CONTENT_HINT);
		roomId = getIntent().getStringExtra(IntentKey.ROOM_ID);
		billId = getIntent().getStringExtra(IntentKey.BILL_ID);
		search_type = getIntent().getStringExtra(IntentKey.SEARCH_TYPE);
		flow_code = getIntent().getStringExtra(IntentKey.FLOW_CODE);
		unit = getIntent().getStringExtra(IntentKey.UNIT);
		maxBackNumber = getIntent().getIntExtra(IntentKey.MAX_BACK_NUMBER, 0);
		output_status = getIntent().getIntExtra(IntentKey.OUTPUT_STATUS, 0);
	}

	@Override
	public void initData()
	{
		customDialogData = new CustomDialogData(CustomDialogActivity.this);
	}

	@Override
	public void initViews()
	{
		int contentViewResId = getLayoutForType(type);
		setContentView(contentViewResId);
		tv_title = (BaseTextView) findViewById(R.id.tv_title);
		tv_content = (BaseEditText) findViewById(R.id.tv_content);
		if (!TextUtils.isEmpty(contentName))
		{
			if (!type.equals(DialogEnum.REMARK.name))
			{
				if (Integer.parseInt(contentName) < 1)
					tv_content.setText("1");
				else
					tv_content.setText(contentName);
			} else
				tv_content.setText(contentName);
		} else
		{
			if (!TextUtils.isEmpty(hint))
			{
				tv_content.setHint(hint);
			} else
				tv_content.setText("1");
		}
		if (!type.equals(DialogEnum.REMARK.name))
			tv_content.setOnClickListener(mOnBaseClickListener);

		btn_sure = (Button) findViewById(R.id.btn_sure);
		btn_sure.setText("确定");
		btn_sure.setOnClickListener(mOnBaseClickListener);
		if (!type.equals(DialogEnum.CUSTOM_VIEW_TITLE.name))
		{
			btn_Cancel = (Button) findViewById(R.id.btn_cancel);
			btn_Cancel.setText("取消");
			btn_Cancel.setOnClickListener(mOnBaseClickListener);
		} else
		{
			btn_sure.setText("下单");
			img_close = (ImageView) findViewById(R.id.img_close);
			img_close.setOnClickListener(mOnBaseClickListener);
		}

		if (!type.equals(DialogEnum.CUSTOM_VIEW_TITLE.name))
		{
			if (!TextUtils.isEmpty(titleName))
			{
				tv_title.setVisibility(View.VISIBLE);
				tv_title.setText(titleName);
			} else
				tv_title.setVisibility(View.GONE);
		} else
		{
			if (!TextUtils.isEmpty(search_type) && search_type.equals(VodConstant.SEARCH))
			{
				tv_title.setVisibility(View.GONE);
				findViewById(R.id.lay_title).setVisibility(View.VISIBLE);
				tv_left_title = (BaseTextView) findViewById(R.id.tv_left_title);
				tv_left_title.setText(titleName);
			} else
			{
				tv_title.setVisibility(View.VISIBLE);
				tv_title.setText(titleName);
				findViewById(R.id.lay_title).setVisibility(View.GONE);
			}
			et_title_content = (BaseEditText) findViewById(R.id.et_title_content);
		}

		if (!type.equals(DialogEnum.REMARK.name))
		{
			currentNumber = Integer.parseInt(contentName);
			if (currentNumber == 0)
				currentNumber = 1;
			tv_name = (BaseTextView) findViewById(R.id.tv_name);
			if (!TextUtils.isEmpty(leftName))
			{
				tv_name.setVisibility(View.VISIBLE);
				tv_name.setText((String) leftName);
			} else
				tv_name.setVisibility(View.GONE);
			tv_cut_1 = (BaseTextView) findViewById(R.id.tv_cut_1);
			tv_add_1 = (BaseTextView) findViewById(R.id.tv_add_1);
			tv_cut_1.setOnClickListener(mOnBaseClickListener);
			tv_add_1.setOnClickListener(mOnBaseClickListener);
		}
	}

	private OnBaseClickListener mOnBaseClickListener = new OnBaseClickListener()
	{
		@Override
		public void onBaseClick(View v)
		{
			switch (v.getId())
			{
			case R.id.btn_sure:
				if (type.equals(DialogEnum.REMARK.name))
				{
					String value = tv_content.getText().toString().trim();
					if (type.equals(DialogEnum.REMARK.name) && !TextUtils.isEmpty(value))
					{
						if (value.contains("#") || value.contains("$") || value.contains("%") || value.contains("^") || value.contains("&")
								|| value.contains("*") || value.contains("<") || value.contains(">"))
						{
							ToastUtils.showText(CustomDialogActivity.this, R.string.error_remark);
							return;
						}
					}
					customDialogData.requestRemark(billId, value);
				} else if (type.equals(DialogEnum.CUSTOM_VIEW_TITLE.name))
				{
					HashMap<String, String> map = new HashMap<String, String>();
					if (!TextUtils.isEmpty(search_type) && search_type.equals("search"))
					{
						if (TextUtils.isEmpty(et_title_content.getText().toString().trim()))
						{
							ToastUtils.makeText(CustomDialogActivity.this, "内容不可为空", ToastUtils.TOAST_TIME).show();
							return;
						}
						map.put("service_content", et_title_content.getText().toString().trim());
					} else
						map.put("service_content", tv_title.getText().toString().trim());
					map.put("service_goods_id", service_goods_id);
					map.put("amount", tv_content.getText().toString());
					map.put("room_id", roomId);
					customDialogData.requestAddService(map);
					SoftKeyboardUtils.closeSoftKeyBoard(CustomDialogActivity.this, et_title_content);
					finish();
					return;
				} else if (type.equals(DialogEnum.CUSTOM_VIEW_BACK.name))
				{
					String content = tv_content.getText().toString();
					if (!TextUtils.isEmpty(content) && Integer.parseInt(content) < 1)
						ToastUtils.showText(CustomDialogActivity.this, "至少退1" + unit);
					else if (!TextUtils.isEmpty(content) && Integer.parseInt(content) > maxBackNumber)
						ToastUtils.showText(CustomDialogActivity.this, "最多可退" + maxBackNumber + unit);
					else
					{
						Intent intent = new Intent();
						intent.putExtra(IntentKey.GOODSINFO, goodInfo);
						intent.putExtra(IntentKey.FLOW_CODE, flow_code);
						intent.putExtra(IntentKey.BACK_NUMBER, content);
						//intent.putExtra(IntentKey.NEED_PERMISSION, false);
						setResult(RESULT_OK, intent);
						finish();
					}
				} else if (type.equals(DialogEnum.CUSTOM_VIEW.name))
				{
					Intent intent = new Intent();
					String content = tv_content.getText().toString();
					intent.putExtra(IntentKey.CUSTOM_DIALOG_CONTENT, content);
					intent.putExtra(IntentKey.CUSTOM_DIALOG_TITLE, titleName);
					setResult(RESULT_OK, intent);
					finish();
				}
				break;
			case R.id.btn_cancel:
				finish();
				break;
			case R.id.img_close:
				finish();
				break;
			case R.id.tv_content:
				KeyBoardDialog keyBoardDialog = new KeyBoardDialog(CustomDialogActivity.this);
				keyBoardDialog.setKeyBoardOnSureListener(CustomDialogActivity.this);
				keyBoardDialog.show();
				break;
			case R.id.tv_add_1:
				onClickAddOne();
				break;
			case R.id.tv_cut_1:
				onClickCutOne();
				break;
			default:
				break;
			}
		}
	};

	/**
	 * 每次增加1个
	 */
	private void onClickAddOne()
	{
		if (type.equals(DialogEnum.CUSTOM_VIEW_BACK.name))
		{
			if (currentNumber < maxBackNumber)
			{
				currentNumber = currentNumber + 1;
			} else
			{
				ToastUtils.showText(context, "最多可退" + maxBackNumber + unit);
			}
		} else
		{
			currentNumber = currentNumber + 1;
		}
		tv_content.setText(String.valueOf(currentNumber));
	}

	/**
	 * 每次减少1个
	 */
	private void onClickCutOne()
	{
		if (currentNumber > 1)
		{
			currentNumber = currentNumber - 1;
		} else
		{
			if (type.equals(DialogEnum.CUSTOM_VIEW_BACK.name))
				ToastUtils.showText(context, "至少退1" + unit);
			else
				ToastUtils.showText(context, "不能再减少了");
		}
		tv_content.setText(String.valueOf(currentNumber));
	}

	/**
	 * @param type
	 * @return
	 */
	private static int getLayoutForType(String type)
	{
		int returnValue = -1;
		if (TextUtils.isEmpty(type))
			return returnValue;

		if (type.equals(DialogEnum.REMARK.name))
			returnValue = R.layout.common_dialog_activity_remark;
		if (type.equals(DialogEnum.CUSTOM_VIEW.name))
			returnValue = R.layout.common_dialog_view_toast;
		if (type.equals(DialogEnum.CUSTOM_VIEW_BACK.name))
			returnValue = R.layout.common_dialog_view_toast;
		if (type.equals(DialogEnum.CUSTOM_VIEW_TITLE.name))
			returnValue = R.layout.common_dialog_view_service;

		return returnValue;
	}

	@Override
	public void setValue(String value)
	{
		if (value.contains("."))
			ToastUtils.showText(CustomDialogActivity.this, R.string.error_number);
		else if (value.length() > 9)
			ToastUtils.showText(CustomDialogActivity.this, R.string.error_max_number);
		else if (Integer.parseInt(value) <= 0)
			ToastUtils.showText(CustomDialogActivity.this, R.string.error_number);
		else
		{
			tv_content.setText(value);
			currentNumber = Integer.parseInt(value);
		}
	}

	public enum DialogEnum
	{

		/**
		 * 备注输入框
		 */
		REMARK("remark"),

		/**
		 * 带有增加减少的输入框
		 */
		CUSTOM_VIEW("custom"),

		/**
		 * 带有增加减少退单
		 */
		CUSTOM_VIEW_BACK("custom_back"),
		/**
		 * 带有增加减少的输入框，且标题也有输入框
		 */
		CUSTOM_VIEW_TITLE("custom_title");

		private String name;

		// 构造方法
		DialogEnum(String name)
		{
			this.name = name;
		}

		@Override
		public String toString()
		{
			return this.name;
		}
	}

}
